package vn.appsmobi.loader;

import android.content.Context;

import java.util.ArrayList;

import org.json.JSONObject;

import vn.appsmobi.model.CategoryInfo;
import vn.appsmobi.ulti.Constant;
import vn.appsmobi.ulti.Request;

public class CategoryLoader extends BaseLoader
{
	private String catid;
    public CategoryLoader(Context paramContext)
    {
        super(paramContext);
    }
    
    protected String getCacheKey()
    {
        return "category";
    }
    
    protected BaseResult getResultInstance()
    {
        return new Result();
    }
    
    protected BaseLoader.UpdateTask getUpdateTask()
    {
        return new CategoryUpdateTask();
    	
    }
    
    public void setCategoryID(String categoryid)
    {
    	this.catid = categoryid;
    }
    
    protected BaseResult parseResult(JSONObject json, BaseResult result)
    throws Exception
    {
        Result localResult = (Result)result;
        localResult.mCategoryInfos = CategoryInfo.valueOf(json);
        return localResult;
    }
    
    private class CategoryUpdateTask extends BaseLoader.UpdateTask
    {
        private CategoryUpdateTask()
        {
            super();
        }

		@Override
		protected Request getRequest() {			
			return new Request(String.format(Constant.GET_CATEGORY_URL,CategoryLoader.this.catid));
		}
             
    }
    
    public static final class Result extends BaseResult
    {
        public ArrayList<CategoryInfo> mCategoryInfos;
        
        protected int getCount()
        {
        	int count = 0;
            if (this.mCategoryInfos != null)
             count = this.mCategoryInfos.size();
            return count;
        }
        
        public BaseResult shallowClone()
        {
            Result localResult = new Result();
            localResult.mCategoryInfos = this.mCategoryInfos;
            return localResult;
        }
    }

	
}